cmake_minimum_required(VERSION 2.6.0 FATAL_ERROR)

project(fnt)
set(PACKAGE_NAME "fnt")
set(PACKAGE_NAME_LONG "Fast Neurite Tracer")
set(PACKAGE_DESCRIPTION "Semi-automatic neurite tracing with tera-bytes of imaging data")
set(PACKAGE_VERSION "0.99.150")
set(PACKAGE_URL "https://bap.cebsit.ac.cn/")
set(PACKAGE_BUGREPORT "https://sourceforge.net/p/fast-neurite-tracer/tickets/")
set(PACKAGE_AUTHORS "Wang Xiaofei <xfwang@ion.ac.cn> GOU Lingfeng <goulf@ion.ac.cn>")
set(PACKAGE_ORG "Lab. of Biological Network Computation, Institute of Neuroscience, China")
set(PACKAGE_DOMAIN "https://bap.cebsit.ac.cn/")
  
string(REPLACE " " "-" PACKAGE_NAME_LONG_LC "${PACKAGE_NAME_LONG}")
string(TOLOWER "${PACKAGE_NAME_LONG_LC}" PACKAGE_NAME_LONG_LC)

if(POLICY CMP0042)
	cmake_policy(SET CMP0042 NEW)
endif()
if(POLICY CMP0020)
	cmake_policy(SET CMP0020 NEW)
endif()
if(POLICY CMP0012)
	cmake_policy(SET CMP0012 NEW)
endif()

if(NOT CMAKE_BUILD_TYPE)
	set(CMAKE_BUILD_TYPE "Debug")
endif()

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
if(${CMAKE_BUILD_TYPE} STREQUAL "Release")
	#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
endif()


if(MINGW)
	add_definitions(-DBUILD_MINGW)
elseif(WIN32)
	add_definitions(-DWIN32 -DBUILD_MINGW)
elseif(APPLE)
	add_definitions(-DBUILD_APPLE)
endif()
set(PACKAGE_PLUGINDIR "lib${LIB_SUFFIX}/${PACKAGE_NAME}-plugins")
set(PACKAGE_DATADIR "share")

set(CMAKE_INSTALL_BINDIR "${CMAKE_INSTALL_PREFIX}/bin")
set(CMAKE_INSTALL_PLUGINDIR "${CMAKE_INSTALL_PREFIX}/${PACKAGE_PLUGINDIR}")
set(CMAKE_INSTALL_DATADIR "${CMAKE_INSTALL_PREFIX}/${PACKAGE_DATADIR}")

configure_file(${CMAKE_SOURCE_DIR}/config-fnt.h.cmake
	${CMAKE_BINARY_DIR}/config-fnt.h @ONLY)
include_directories(${CMAKE_BINARY_DIR})

option(USE_OPENMP "Use OpenMP parallelization." ON)
option(BUILD_TOOLS "Build auxiliary tools." ON)
option(BUILD_TRACER "Build the tracer." ON)
option(BUILD_VIEWER "Build the tracer." ON)
option(BUILD_PYLIB "Build python lib." ON)
option(WITH_VP9 "Build with the VP9 codec." ON)
option(WITH_HEVC "Build with the HEVC codec." ON)

if(NOT BUILD_TOOLS AND NOT BUILD_TRACER AND NOT BUILD_VIEWER)
	message(FATAL_ERROR "Nothing to build.")
endif()

if(USE_OPENMP)
	if(APPLE)
	set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include")
	set(OpenMP_C_LIB_NAMES "libomp")
	set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp -I/opt/homebrew/opt/libomp/include")
	set(OpenMP_CXX_LIB_NAMES "libomp")
	set(OpenMP_libomp_LIBRARY "/opt/homebrew/opt/libomp/lib/libomp.dylib")
	endif()
	find_package(OpenMP REQUIRED)
	add_definitions(-DUSE_OPENMP)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")

endif()

if(WIN32)
find_package(Deps REQUIRED)
find_package(Qt5 5.4 REQUIRED COMPONENTS Core Widgets Gui Network Multimedia)
else()
	find_package(ZLIB REQUIRED)
find_package(TIFF REQUIRED)
if(BUILD_TRACER OR BUILD_VIEWER)
	find_package(CURL REQUIRED)
	find_package(Qt5 5.4 REQUIRED COMPONENTS Core Widgets Gui Network )
endif()
endif()


if(WIN32)
	include_directories(${Deps_DIR}/zlib/include
	${Deps_DIR}/tiff/include
	${Deps_DIR}/3rdParty/v140-x64/include/
	)
	set(CURL_INCLUDE_DIR "${Deps_DIR}/curl-8.11/")
	set(CURL_LIBRARIES_DIR "${Deps_DIR}/curl-8.11/lib")
	set(CURL_LIBRARIES_DEBUG "libcurl_a_debug")
	set(CURL_LIBRARIES_RELEASE "libcurl_a")
	set(TIFF_LIBRARIES "tiff")
	set(ZLIB_LIBRARIES "zlib")
	if(WITH_VP9)
			set(VPX_INCLUDE_DIRS "${Deps_DIR}/vpx/include" CACHE INTERNAL "")
			set(VPX_LIBRARIES_DIRS "${Deps_DIR}/vpx/lib" CACHE INTERNAL "")
			set(VPX_LIBRARIES "libvpx" CACHE INTERNAL "")
			set(USE_PRIV_VPX TRUE)


			set(WEBM_INCLUDE_DIRS "${Deps_DIR}/webm/include" CACHE INTERNAL "")
			set(WEBM_LIBRARIES_DIRS "${Deps_DIR}/webm/lib" CACHE INTERNAL "")
			set(WEBM_LIBRARIES "libwebm" CACHE INTERNAL "")
			set(USE_PRIV_WEBM TRUE)
		add_definitions(-DWITH_VP9)

	endif()
	
	link_directories(${VPX_LIBRARIES_DIRS} ${WEBM_LIBRARIES_DIRS} ${CURL_LIBRARIES_DIR}
	${Deps_DIR}/tiff/lib
	${Deps_DIR}/zlib/lib

	)
else()
	if(WITH_VP9)
		find_package(PkgConfig)
		if(PkgConfig_FOUND)
			# pkg_check_modules(VPX vpx)
			pkg_check_modules(WEBM webm)
		else()
			set(VPX_FOUND OFF)
			set(WEBM_FOUND OFF)
		endif()
		# MINGW libvpx no high bitdepth
		if( MINGW)
			find_program(YASM_OR_NASM NAMES yasm nasm)
			if(NOT YASM_OR_NASM)
				message(FATAL_ERROR "Building libvpx requires yasm or nasm.")
			endif()
			set(VPX_TAG "v1.7.0")
			include(ExternalProject)
			ExternalProject_Add(priv-vpx
				PREFIX "external/vpx"
				URL "https://github.com/webmproject/libvpx/archive/${VPX_TAG}.tar.gz"
				CONFIGURE_COMMAND "<SOURCE_DIR>/configure" "--disable-install-bins"
				"--disable-install-libs" "--disable-examples" "--disable-tools"
				"--disable-docs" "--enable-vp9-highbitdepth" "--enable-vp9"
				"--disable-unit-tests" "--disable-webm-io" "--disable-libyuv"
				BUILD_COMMAND "$(MAKE)" "libvpx.a"
				INSTALL_COMMAND ""
				)
			ExternalProject_Get_Property(priv-vpx binary_dir source_dir)
			set(VPX_INCLUDE_DIRS "${source_dir}" CACHE INTERNAL "")
			set(VPX_LIBRARIES "${binary_dir}/libvpx.a" CACHE INTERNAL "")
			set(USE_PRIV_VPX TRUE)
		endif()
		if(NOT WEBM_FOUND)
			set(WEBM_TAG "libwebm-1.0.0.27")
			include(ExternalProject)
			ExternalProject_Add(priv-webm
				PREFIX "external/webm"
				URL "https://github.com/webmproject/libwebm/archive/${WEBM_TAG}.tar.gz"
				CMAKE_ARGS "-DCMAKE_BUILD_TYPE=Release"
				BUILD_COMMAND "$(MAKE)" "webm"
				INSTALL_COMMAND ""
				)
			ExternalProject_Get_Property(priv-webm binary_dir source_dir)
			set(WEBM_INCLUDE_DIRS "${source_dir}" CACHE INTERNAL "")
			set(WEBM_LIBRARIES "${binary_dir}/libwebm.a" CACHE INTERNAL "")
			set(USE_PRIV_WEBM TRUE)
		endif()
		add_definitions(-DWITH_VP9)
	endif()
endif()

if(WIN32)
	if(WITH_HEVC)
		set(HEVC_INCLUDE_DIRS "${Deps_DIR}/hevc/include" CACHE INTERNAL "")
		set(HEVC_LIBRARIES_DIRS "${Deps_DIR}/hevc/lib" CACHE INTERNAL "")
			set(HEVC_LIBRARIES
				"TLibCommon"
				"TLibDecoder"
				CACHE INTERNAL "")
			set(USE_PRIV_HEVC TRUE)
			add_definitions(-DWITH_HEVC)
	link_directories(${HEVC_LIBRARIES_DIRS} )

	endif()
else()
	if(WITH_HEVC)
		#find_program(YASM_OR_NASM NAMES yasm nasm)
		#if(NOT YASM_OR_NASM)
		#message(FATAL_ERROR "Building libvpx requires yasm or nasm.")
		#endif()

		include(ExternalProject)
		ExternalProject_Add(priv-hevc
			PREFIX "external/hevc"
			SVN_REPOSITORY "http://hevc.hhi.fraunhofer.de/svn/svn_HEVCSoftware/tags/HM-16.20/"
			SVN_TRUST_CERT 1
			UPDATE_COMMAND ""
			PATCH_COMMAND "patch" "-p1" "-i" "${CMAKE_SOURCE_DIR}/xxx-hm.patch"
			CONFIGURE_COMMAND ""
			BUILD_COMMAND "$(MAKE)" "-C" "../priv-hevc/build/linux/"
	"CPP=${CMAKE_CXX_COMPILER}" "CC=${CMAKE_C_COMPILER}"
	"release_highbitdepth"
			INSTALL_COMMAND ""
			)
		ExternalProject_Get_Property(priv-hevc binary_dir source_dir)
		set(HEVC_INCLUDE_DIRS "${source_dir}/source/Lib" CACHE INTERNAL "")
		set(HEVC_LIBRARIES
			"${source_dir}/lib/libTLibDecoderHighBitDepthStatic.a"
			"${source_dir}/lib/libTLibCommonHighBitDepthStatic.a"
			CACHE INTERNAL "")
		set(USE_PRIV_HEVC TRUE)
		add_definitions(-DWITH_HEVC)
	endif()
endif()

SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/install)
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/install)
SET(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/build/install)

add_subdirectory(shared)

if(BUILD_TOOLS)
	add_subdirectory(tools)
endif()

if(BUILD_TRACER)
	add_subdirectory(tracer)
endif()

if(BUILD_VIEWER)
	add_subdirectory(viewer)
endif()
if(BUILD_PYLIB AND WIN32)

	add_subdirectory(pylib)
endif()


